$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'lm/goods/getGoodsInDetail');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del'], ['bt_list_editPrice']);
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //双击
            $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
            };
            //增加
            $('#bt_list_add').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().add();
            });
            //编辑价格
            $('#bt_list_editPrice').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().editPrice();
            });
            //删除
            $('#bt_list_del').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().del();
            })
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        inType: xhExtra.getValue($('#list_search_inType')),
                        goodsType: xhExtra.getValue($('#list_search_goodsType')),
                        status: xhExtra.getValue($('#list_search_status')),
                        consume: xhExtra.getValue($('#list_search_consume'))
                    })
                },
                add: function () {
                    window.top.xhTop.showModal({
                        title: '物品登记',
                        content: xhTool.getFrameString('lm/goods/addGoodsIn', null, 350),
                        button: '保存',
                        size: 'md',
                        width: '600',
                        backClose: false
                    });
                },
                editPrice: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var old_price = $this.dataGrid.getSelectValue('iInPrice');
                    if (id < 1){
                        window.top.xhTop.showError('未选中记录');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写价格',
                        html: '<div><input id="price" type="number" class="swal2-input" placeholder="必填" value="'+old_price+'"></div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var price = $('#price', window.top.document).val();
                                if (price === ''){
                                    reject('请填写价格');
                                    return false;
                                } else {
                                    xhExtra.ajaxRequest('lm/goods/modifyGoodsPrice', {
                                        id: id,
                                        price: price
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                },
                del:function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length < 1){
                        window.top.xhTop.showError('未选中记录');
                        return false;
                    }
                    xhExtra.askChangeStatus('lm/goods/delGoodsInData', {ids: ids}, $this.dataGrid, '确认删除？');
                }
            }
        }
    }
};